package com.ruitu.mall_98_2.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.AppUtils;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.utils.UIUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.Version;

import cn.jpush.android.api.JPushInterface;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SettingActivity extends MyBaseActivity {
    private TextView tv_log_out;
    private RelativeLayout rl_invite_friends;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("设置");
        findViewById(R.id.rl_about).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AboutActivity.class);
            }
        });

        findViewById(R.id.rl_check_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateVersion();
            }
        });
        tv_log_out = findViewById(R.id.tv_log_out);
        rl_invite_friends = findViewById(R.id.rl_invite_friends);
        if (CommonUtils.isLogin()) {
            tv_log_out.setVisibility(View.VISIBLE);
        } else {
            tv_log_out.setVisibility(View.GONE);
        }
        tv_log_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHolder.instance().setUser(null);
                Arad.preferences.putString("psw", "").flush();
                Arad.preferences.putString("type", "").flush();
                Arad.bus.post(new EventModel(180102500));
                JPushInterface.setAlias(Arad.app, 0, "1111111110");
                finish();
            }
        });
        rl_invite_friends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.isLoginToActivity(SettingActivity.this)){
                    startActivity(InviteFriendsActivity.class);
                }
            }
        });
    }

    private void updateVersion() {
        APIRetrofit.getDefault().checkOne()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult<Version>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        e.printStackTrace();
                        Logg.i(e.toString());
                        ToastUtils.showShort("获取更新失败");
                    }

                    @Override
                    public void onNext(BaseResult<Version> versionBaseResult) {
                        hideProgress();
                        if (versionBaseResult.isSuccess()) {
                            if (versionBaseResult.getData() != null) {
                                if (versionBaseResult.getData().getCode() > AppUtils.getAppVersionCode()) {
                                    showUpdataViewDialog(versionBaseResult.getData());
                                } else {
                                    ToastUtils.showShort("已是最新版本");
                                }
                            } else {
                                ToastUtils.showShort("已是最新版本");
                            }
                        } else {
                            ToastUtils.showShort(versionBaseResult.getMsg());
                        }
                    }
                });
    }

    private void showUpdataViewDialog(final Version data) {
        UIUtils.showAlertDialog(getSupportFragmentManager(), "发现新版本",
                data.getContent(), "立即更新", "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Config.BASE_URL + data.getUrl()));
                        startActivity(intent);
                    }
                }
                , null);
    }
}
